package client;

import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import data.*;

/**
 * User: Inbal
 * Date: 22/03/2009
 * Time: 18:03:09
 */
public class GoOutServer {
    private static GoOutService_PortType goOutService;

    static {
        try {
            goOutService = new GoOutServiceServiceLocator().getGoOutService(new URL("http://localhost:8080/axis/services/GoOutService"));
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public static BooleanValue allreadyResgistered(String reservationName, Date date) {
        try {
            boolean value = goOutService.allreadyResgistered(reservationName, date);
            return new BooleanValue(true, value, null);
        } catch (RemoteException e) {
            return new BooleanValue(false, false, e.getMessage());
        }
    }

    public static StringListValue getRestaurantTypes() {
        LinkedList<String> res = new LinkedList<String>();
        try {
            String[] types = goOutService.getRestaurantTypes();
            if (types != null) {
                res.addAll(Arrays.asList(types));
            }
            return new StringListValue(true,res,null);
        } catch (RemoteException e) {
            return new StringListValue(false,res,e.getMessage());
        }
    }

    public static StringListValue getMovies() {
        LinkedList<String> res = new LinkedList<String>();
        try {
            String[] movies = goOutService.getMovies();
            if (movies != null) {
                res.addAll(Arrays.asList(movies));
            }
            return new StringListValue(true,res,null);
        } catch (RemoteException e) {
            return new StringListValue(false,res,e.getMessage());
        }
    }

    public static ReservationValue reserve(ReservationPlan plan) {
        String reservationName = plan.getReservationName();
        String movieName = plan.getMovieName();
        RestaurantType restaurantType = new RestaurantType(plan.getRestaurantType());
        Time time = new Time(String.valueOf(plan.getTime()));
        Date date = plan.getDate();
        int ticketCount = plan.getTicketCount();
        try {
            goOutService.makeAplanToGoOut(reservationName, movieName, restaurantType, time, date, ticketCount);
            NightOutDetails theResults = goOutService.getTheResults(reservationName, date);
            ReservationResponse response = new ReservationResponse(theResults.isSuccessful(), theResults.getErrorDescription());
            return new ReservationValue(true, response, null);
        } catch (RemoteException e) {
            return new ReservationValue(false, null, e.getMessage());
        }
    }
}
